﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using AppLayer;
using SpotItComponents;
using SpotItServerAPI;

namespace SpotIt
{
    /// <summary>
    /// Interaction logic for SpotIt_Multi_CreateGame.xaml
    /// </summary>
    public partial class SpotIt_Multi_CreateGame : Window
    {
        private Services services;
        private Session session;
        private List<PlayingDeck> decks;
        public GameMonitor myGameMonitor { get; set; }
        public bool canceled = true;
        AudioFiles theAudioFiles = AudioFiles.Instance;
        public SpotIt_Multi_CreateGame()
        {
            InitializeComponent();
        }

        public SpotIt_Multi_CreateGame(Session session, Services services)
        {
            InitializeComponent();

            this.session = session;
            this.services = services;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            deckDataGrid.CanUserAddRows = false;
            deckDataGrid.CanUserDeleteRows = false;

            gameTypeComboBox.Items.Add("Hot Potato");
            gameTypeComboBox.Items.Add("Poison");
            gameTypeComboBox.Items.Add("Tower");
            gameTypeComboBox.Items.Add("Well");

            gameTypeComboBox.SelectedIndex = 0;

            OperationResult result = services.deckManager.AvailablePlayingDecks(session);
            if (result.Success)
                decks = result.Data as List<PlayingDeck>;

            deckDataGrid.ItemsSource = decks;

        }

        private void createButton_Click(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[2].Play();
            canceled = false;

            GameType gameType = GameType.HotPotato;
            switch (gameTypeComboBox.SelectedIndex)
            {
                case 0:
                    gameType = GameType.HotPotato;
                    break;
                case 1:
                    gameType = GameType.Poison;
                    break;
                case 3:
                    gameType = GameType.Tower;
                    break;
                case 4:
                    gameType = GameType.Well;
                    break;
            }

            myGameMonitor = new GameMonitor();
            PlayingDeck selectedDeck = new PlayingDeck();
            if (deckDataGrid.SelectedItem != null)
                selectedDeck = (PlayingDeck)deckDataGrid.SelectedItem;
            services.gameManager.CreateGame(session, selectedDeck, gameType, titleTextBox.Text, (int)mublerOfPlayerSlider.Value, myGameMonitor);


            this.Close();
        }

        private void mublerOfPlayerSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (playerCountLabel != null)
            {
                playerCountLabel.Content = ((int)mublerOfPlayerSlider.Value).ToString();
                playerCountLabel.InvalidateVisual();
            }
            
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[2].Play();
            canceled = true;
            this.Close();
        }

        
    }
}
